import { createRouter, createWebHistory } from 'vue-router'
import { useLoginPinia } from '@/stores/index'
import useFenLeiPinia from '@/stores/models/useFenLeiPinia'
import useTextPinia from '@/stores/models/useTextPinia'
import { useMyInformationPinia } from '@/stores/index'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/login' ,
      component: () => import('@/views/layout/layOut.vue'),
      children:[
        {
          path: '/fenlei',
          component: () => import('@/views/fenLei/fenLei.vue'),
        },
        {
          path: '/guanli',
          component: () => import('@/views/guanLi/guanLi.vue'),
        },
        {
          path: '/mima',
          component: () => import('@/views/miMa/miMa.vue'),
        },
        {
          path: '/touxiang',
          component: () => import('@/views/touXiang/touXiang.vue'),
        },
        {
          path: '/ziliao',
          component: () => import('@/views/ziLiao/ziLiao.vue'),
        },
      ]
    },
    {
      path: '/login',
      component: () => import('@/views/login/myLogin.vue'),
    }
  ],
})

//导航守卫
router.beforeEach((to) => {
  const loginPinia = useLoginPinia()
  console.log('isLogin值',loginPinia.isLogin)
  if ( !loginPinia.isLogin && to.path != '/login') {
    console.log('没有登陆')
    return '/login'
  }
  if ( to.path === '/login' ) {
    useFenLeiPinia().updateFenLeiString('')
    useLoginPinia().updateIsLogin('')
    useMyInformationPinia().updateMyInformation('')
    useTextPinia().updateTextList('')
    return console.log('退出登录，数据清空')
  }
  return console.log('可以切换路由')
})
export default router
